package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingProjectDto;
import com.zzyl.entity.NursingProject;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.service.NursingProjectService;
import com.zzyl.vo.NursingProjectPlanVo;
import com.zzyl.vo.NursingProjectVo;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class NursingProjectServiceImpl implements NursingProjectService {

    @Autowired
    private NursingProjectMapper nursingProjectMapper;
    /**
     * 分页查询护理项目信息
     * @param name
     * @param pageNum
     * @param pageSize
     * @param status
     * @return
     */
    @Override
    public PageResponse<NursingProjectVo> getByPage(String name, Integer pageNum, Integer pageSize, Integer status) {
        PageHelper.startPage(pageNum , pageSize);

        Page<NursingProjectVo> page = nursingProjectMapper.selectByPage(name,status);
        //设置count值
        for(NursingProjectVo nursingProjectVo : page){
           List<NursingProjectPlanVo> nursingProjectPlanVos = nursingProjectMapper.getProjectPlan(nursingProjectVo.getId());
           if(nursingProjectPlanVos.isEmpty()){
               nursingProjectVo.setCount(0);
           }else{
               nursingProjectVo.setCount(1);
           }
        }

        PageResponse<NursingProjectVo> pageResponse = new PageResponse<>(page);
        pageResponse.setRecords(page.getResult());

        return pageResponse;
    }

    /**
     * 新增护理项目
     * @param nursingProjectDto
     */
    @Override
    public void add(NursingProjectDto nursingProjectDto) {
        NursingProject nursingProject = new NursingProject();
        BeanUtil.copyProperties(nursingProjectDto,nursingProject);
        nursingProjectMapper.add(nursingProject);
    }


    @Override
    public NursingProjectVo getById(Long id) {

        return nursingProjectMapper.getById(id);
    }

    @Override
    public void update(NursingProjectDto nursingProjectDto) {
        NursingProject nursingProject = new NursingProject();
        BeanUtil.copyProperties(nursingProjectDto,nursingProject);

        nursingProjectMapper.update(nursingProject);
    }

    /**
     * 查询所有护理项目
     *
     * @return
     */
    @Override
    public List<NursingProjectVo> getAllProject() {
        List<NursingProjectVo> nursingProjectVos =  nursingProjectMapper.getAllProject();
        return nursingProjectVos;
    }

    /**
     * 护理项目的禁用启用
     *
     * @param id     护理项目id
     * @param status 护理项目状态
     * @return
     */
    @Override
    public void updateStatus(Long id, Integer status) {
        nursingProjectMapper.updateStatus(id,status);
    }

    /**
     * 删除护理项目
     *
     * @param id 护理项目id
     * @return
     */
    @Override
    public void deleteProject(Long id) {
        nursingProjectMapper.deleteProject(id);
    }

}
